/**
 * @Title: DateTimeUtil.java
 * @Package com.alinkpay.utils
 * @Description: TODO Copyright: Company:
 * @author Hunter.Pan
 * @date 2015年9月23日 上午8:55:06
 * @version
 */
package com.shopx.core.utils;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Hunter.Pan hunter.pan@foxmail.com
 * @ClassName: DateTimeUtil
 * @Description: 日期时间工具类
 * @date 2015年10月5日 下午8:05:09
 */
public final class DateTimeUtil {
  /**
   * @return String
   * @throws @Date 2015年10月5日下午8:04:38
   * @Title: getDateTime14
   * @Description: 获取当前时间格式为yyyyMMddHHmmss
   * @Author Hunter.Pan hunter.pan@foxmail.com
   */
  public static String getDateTime14() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    return sdf.format(new Date());
  }

  /**
   * 获取本月第一天的时间
   *
   * @author songmin
   * @date 2021/7/25 16:23
   * @return: java.lang.String
   */
  public static String getTimeByMonthFirstDay() {
    Calendar cale = Calendar.getInstance();
    cale.set(Calendar.DAY_OF_MONTH, 1);
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    return format.format(cale.getTime()) + "000000";
  }

  /**
   * 获取当前时间格式为HHmmss
   *
   * @author songmin
   * @date 2021/3/3 17:13
   * @return: java.lang.String
   */
  public static String getDateTime6() {
    return getDateTime14().substring(8);
  }

  public static String format(Date date, String fmt) {
    SimpleDateFormat sdf = new SimpleDateFormat(fmt);
    return sdf.format(date);
  }

  public static Date parse(String dt, String fmt) {
    SimpleDateFormat sdf = new SimpleDateFormat(fmt);
    Date date = null;
    try {
      date = sdf.parse(dt);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  /**
   * @param date  源日期
   * @param fmt   格式
   * @param field 字段(Y|M|D)
   * @param value 正数为加, 负数为减
   * @return String
   * @throws @Date 2015年10月5日下午7:55:41
   * @Title: dateCalc
   * @Description: 计算日期
   * @Author Hunter.Pan hunter.pan@foxmail.com
   */
  public static String dateCalc(String date, String fmt, String field, Integer value) {
    try {
      SimpleDateFormat sdf = null;
      if (StringUtils.isEmpty(fmt)) {
        sdf = new SimpleDateFormat("yyyyMMdd");
      } else {
        sdf = new SimpleDateFormat(fmt);
      }
      Calendar cd = Calendar.getInstance();
      cd.setTime(sdf.parse(date));
      if ("Y".equals(field)) {
        cd.add(Calendar.YEAR, value);
      } else if ("M".equals(field)) {
        cd.add(Calendar.MONTH, value);
      } else if ("D".equals(field)) {
        cd.add(Calendar.DATE, value);
      } else {
        throw new IllegalArgumentException("参数错误(支持Y,M,D)");
      }
      return sdf.format(cd.getTime());
    } catch (ParseException ex) {

    }
    return null;
  }


  public static String dateTimeCalc(String date, String fmt, String field, Integer value) {
    try {
      SimpleDateFormat sdf = null;
      if (StringUtils.isEmpty(fmt)) {
        sdf = new SimpleDateFormat("yyyyMMddHHmmss");
      } else {
        sdf = new SimpleDateFormat(fmt);
      }

      Calendar cd = Calendar.getInstance();
      cd.setTime(sdf.parse(date));
      if ("Y".equals(field)) {
        cd.add(1, value);
      } else if ("M".equals(field)) {
        cd.add(2, value);
      } else if ("D".equals(field)) {
        cd.add(5, value);
      } else if ("H".equals(field)) {
        cd.add(10, value);
      } else if ("MI".equals(field)) {
        cd.add(12, value);
      } else {
        if (!"S".equals(field)) {
          throw new IllegalArgumentException("参数错误(支持Y,M,D,H,MI,S)");
        }

        cd.add(13, value);
      }

      return sdf.format(cd.getTime());
    } catch (ParseException var6) {
      return null;
    }
  }

  public static Date dateCalc(Date date, String fmt, String field, Integer value) {
    try {
      Calendar cd = Calendar.getInstance();
      cd.setTime(date);
      if ("Y".equals(field)) {
        cd.add(Calendar.YEAR, value);
      } else if ("M".equals(field)) {
        cd.add(Calendar.MONTH, value);
      } else if ("D".equals(field)) {
        cd.add(Calendar.DATE, value);
      } else {
        throw new IllegalArgumentException("参数错误(支持Y,M,D)");
      }
      return cd.getTime();
    } catch (Exception ex) {

    }
    return null;
  }

  /**
   * @return String
   * @Title: getDate8
   * @Description: 获取当前日期
   * @Author Hunter.Pan hunter.pan@foxmail.com
   * @Date 2015年10月5日下午7:55:41
   */
  public static String getDate8() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Date today = new Date();
    return sdf.format(today);
  }

  /**
   * 获取当前月份
   *
   * @param
   * @return java.lang.String
   * @author lituo
   * @date 10:53 AM 2022/2/7
   */
  public static String getMonth() {
    return getDate8().substring(0, 6);
  }

  public static String getTime() {
    SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
    Date today = new Date();
    return sdf.format(today);
  }

  public static long getDaySub(Date beginDate, Date endDate) {
    long day = 0;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    try {
      beginDate = format.parse(format.format(beginDate));
      endDate = format.parse(format.format(endDate));
      day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return day;
  }

  /**
   * 取得前/后 分钟的日期
   *
   * @param date 需要计算的日期
   * @param time 分钟数（+ 计算日期后的分钟数；- 计算日期前的分钟数）
   * @return
   */
  public static Date getCalcMinute(Date date, int time) {
    long min = time * 60 * 1000;
    return new Date(date.getTime() + min);
  }

  /**
   * 取得前/后 天的日期
   *
   * @param date
   * @param day
   * @return
   */
  public static Date getCalcDay(Date date, int day) {
    long min = day * 24 * 60 * 60 * 1000;
    return new Date(date.getTime() + min);
  }

  /**
   * 取得当月的第一天
   *
   * @return
   */
  public static Date getFirstDay() {
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MONTH, 0);
    c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    return c.getTime();
  }

  /**
   * @param beginTm
   * @param endTm
   * @return long
   * @Title getDistanceTime
   * @Description 计算两个日期相差多少秒
   * @Author lituo
   * @Date 2018年9月13日 上午10:36:02
   */
  public static long getDistanceTime(String beginTm, String endTm) {
    DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    Date one;
    Date two;
    long day = 0;
    long hour = 0;
    long min = 0;
    long sec = 0;
    try {
      one = df.parse(beginTm);
      two = df.parse(endTm);
      long time1 = one.getTime();
      long time2 = two.getTime();
      long diff;
      diff = time2 - time1;
      day = diff / (24 * 60 * 60 * 1000);
      hour = (diff / (60 * 60 * 1000) - day * 24);
      min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
      sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
    } catch (ParseException e) {
      e.printStackTrace();
    }
//    return day + "天" + hour + "小时" + min + "分" + sec + "秒";
    return day * 24 * 60 * 60 + hour * 60 * 60 + min * 60 + sec;
  }

  /**
   * 计算两个时间差距多少小时
   *
   * @return long
   * @author lituo
   * @date 3:56 PM 2022/2/12
   * @param  beginTm
   * @param  endTm
   */
  public static Double getDistanceHour(String beginTm, String endTm) {
    DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    Date one;
    Date two;
    long day = 0;
    long hour = 0;
    long min = 0;
    long sec = 0;
    try {
      one = df.parse(beginTm);
      two = df.parse(endTm);
      long time1 = one.getTime();
      long time2 = two.getTime();
      long diff ;
      if(time1<time2) {
        diff = time2 - time1;
      } else {
        diff = time1 - time2;
      }
      day = diff / (24 * 60 * 60 * 1000);
      hour = (diff / (60 * 60 * 1000) - day * 24);
      min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
      sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return day*24 + hour + new Double(min)/60;
  }

  public static long getDistanceDay(String beginDt, String endDt) {
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    Date one;
    Date two;
    long day = 0;
    try {
      one = df.parse(beginDt);
      two = df.parse(endDt);
      long time1 = one.getTime();
      long time2 = two.getTime();
      long diff;
      diff = time2 - time1;
      day = diff / (24 * 60 * 60 * 1000);
    } catch (ParseException e) {
      e.printStackTrace();
    }
//    return day + "天" + hour + "小时" + min + "分" + sec + "秒";
    return day;
  }


  public static String getFirstDtByLastMonth() {
    //获取前一个月第一天
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, -1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    String firstDay = sdf.format(calendar.getTime());
    return firstDay;
  }

  public static String getLastDtByLastMonth() {
    //获取前一个月最后一天
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, 0);
    String lastDay = sdf.format(calendar.getTime());
    return lastDay;
  }


  public static String getLastMonth() {
    //获取前一个月第一天
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, -1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    String firstDay = sdf.format(calendar.getTime());
    return firstDay;
  }

  public static String getMonthLastDay() {
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    Calendar ca = Calendar.getInstance();
    ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
    String lastDay = format.format(ca.getTime());
    return lastDay;
  }

  /**
   * 根据偏移量获取月份
   *
   * @param offset
   * @return 月份
   */
  public static Integer getMonth(int offset) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, offset);
    SimpleDateFormat format = new SimpleDateFormat("MM");
    String time = format.format(calendar.getTime());
    return Integer.valueOf(time);
  }

  /**
   * 根据给定的偏移量获取月份有多少天
   *
   * @param offset:
   * @author songmin
   * @date 2021/6/23 14:16
   * @return: java.lang.Integer
   */
  public static Integer getDaysOfMonth(int offset) {
    SimpleDateFormat sdf = new SimpleDateFormat("MM");
    try {
      Date date = sdf.parse(String.valueOf(getMonth(offset)));
      return getDaysOfMonth(date);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 根据日期获取星期几
   *
   * @param date 指定日期 如 2020-09-17
   * @return
   */
  public static Integer getDateOfWeek(String date) {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    try {
      c.setTime(simpleDateFormat.parse(date));
    } catch (Exception e) {
      e.printStackTrace();
    }
    return (c.get(Calendar.DAY_OF_WEEK) - 1);
  }

  /**
   * 获取当前是几点
   *
   * @param day
   * @return
   */
  public static Integer getDateOfHour(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, day);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
    String str = format.format(calendar.getTime());
    String hour = str.substring(str.length() - 2);
    return Integer.valueOf(hour);
  }

  /**
   * 获取从当前开始的任意一天
   *
   * @param day 距离当天的天数
   * @return yyyy-MM-dd
   */
  public static String getDateByDay(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, day);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    String str = format.format(calendar.getTime());
    return str;
  }

  /**
   * 获取现在的日期
   *
   * @return yyyy-MM-dd
   */
  public static String getDate() {
    return getDateByDay(0);
  }


  /**
   * 获取现在时间
   *
   * @param day 距离当天的天数
   * @return
   */
  public static String getCurrentTime(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, day);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String str = format.format(calendar.getTime());
    return str;
  }

  /**
   * 获取当前天
   *
   * @param day: 距离的天数
   * @author songmin
   * @date 2021/4/18 14:09
   * @return: java.lang.String
   */
  public static String getCurrentDay(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, day);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String str = format.format(calendar.getTime());
    return str;
  }

  /**
   * 获取今天是几号
   *
   * @param day 距离今天天数
   * @return
   */
  public static Integer getDateOfToday(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, day);
    SimpleDateFormat format = new SimpleDateFormat("dd");
    String time = format.format(calendar.getTime());
    return Integer.valueOf(time);
  }

  /**
   * 获取今年是哪年
   *
   * @param day
   * @return
   */
  public static Integer getDateOfYear(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.YEAR, day);
    SimpleDateFormat format = new SimpleDateFormat("yyyy");
    String time = format.format(calendar.getTime());
    return Integer.valueOf(time);
  }

  /**
   * 获取现在是几月
   *
   * @param day
   * @return
   */
  public static Integer getDateOfMonth(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, day);
    SimpleDateFormat format = new SimpleDateFormat("MM");
    String time = format.format(calendar.getTime());
    return Integer.valueOf(time);
  }

  /**
   * 根据给定的日期获取该日期本月有多少天
   *
   * @param date
   * @return
   */
  public static int getDaysOfMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  }

  /**
   * 根据给定的日期获取该日期本月有多少天
   *
   * @param date
   * @return
   */
  public static int getDaysOfMonth(String date) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return getDaysOfMonth(sdf.parse(date));
  }

  /**
   * 获取从当前开始的任意一天
   *
   * @param day 距离当天的天数
   * @return
   */
  public static String getDay(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, day);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    String str = format.format(calendar.getTime());
    return str;
  }

  /**
   * 获取今天日期 yyyy:MM:dd
   *
   * @author songmin
   * @date 2021/3/2 14:53
   * @return: java.lang.String
   */
  public static String getToday() {
    return getDay(0);
  }

  /**
   * 获取时间24小时制的时间
   *
   * @param day 指定哪天，今天为0,加减推移天数
   * @return
   */
  private static String getTime(int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, day);
    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
    return format.format(calendar.getTime());
  }

  /**
   * 获取现在的时间 24小时制 HH:mm:ss
   *
   * @author songmin
   * @date 2021/3/2 14:56
   * @return: java.lang.String
   */
  public static String getTimeByNow6() {
    return getTime(0);
  }

  /**
   * 获取本月第一天的日期
   *
   * @author songmin
   * @date 2021/6/22 13:51
   * @return: java.lang.String
   */
  public static String getDateByMonthFirstDay() {
    Calendar cale = Calendar.getInstance();
    cale.set(Calendar.DAY_OF_MONTH, 1);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    return format.format(cale.getTime());
  }

  /**
   * 获取本月最后一天的日期
   *
   * @author songmin
   * @date 2021/6/22 14:01
   * @return: java.lang.String
   */
  public static String getDateByMonthLastDay() {
    Calendar cale = Calendar.getInstance();
    cale.set(Calendar.DAY_OF_MONTH, getDaysOfMonth(new Date()));
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    return format.format(cale.getTime());
  }

  /**
   * 获取下个月第一天的日期
   *
   * @author songmin
   * @date 2021/6/22 14:22
   * @return: java.lang.String
   */
  public static String getDateByNextMonthFirstDay() {
    Calendar cale = Calendar.getInstance();
    cale.add(Calendar.DAY_OF_MONTH, getDaysOfMonth());
    cale.set(Calendar.DAY_OF_MONTH, 1);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    return format.format(cale.getTime());
  }


  public static String getFirstDayByNextMth() {
    Calendar cale = Calendar.getInstance();
    cale.add(Calendar.DAY_OF_MONTH, getDaysOfMonth());
    cale.set(Calendar.DAY_OF_MONTH, 1);
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    return format.format(cale.getTime());
  }

  /**
   * description 获取上一个月第一天
   * date 2021/7/25 11:45 AM
   *
   * @param
   * @return java.lang.String
   * @author lituo
   */
  public static String getDateByBeforeMonthFirstDay() {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, -1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    String firstDay = format.format(calendar.getTime());
    return firstDay;
  }

  /**
   * 获取下个月最后一天的日期
   *
   * @author songmin
   * @date 2021/7/22 14:24
   * @return: java.lang.String
   */
  public static String getDateByNextMonthLastDay() {
    Calendar cale = Calendar.getInstance();
    cale.add(Calendar.DAY_OF_MONTH, getDaysOfMonth(1));
    cale.set(Calendar.DAY_OF_MONTH, getDaysOfMonth(1));
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    return format.format(cale.getTime());
  }

  /**
   * 获取本月有多少天
   *
   * @return 天数
   */
  public static Integer getDaysOfMonth() {
    return getDaysOfMonth(new Date());
  }


  /**
   * @param dtm
   * @return java.lang.String
   * @descript 格式化时间戳
   * @author lituo
   * @date 2021/6/8 11:18
   */
  public static String formatDtm(String dtm) {
    String year = dtm.substring(0, 4);
    String month = dtm.substring(4, 6);
    String day = dtm.substring(6, 8);
    String hour = dtm.substring(8, 10);
    String min = dtm.substring(10, 12);
    String sec = dtm.substring(12, 14);
    return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
  }

  /**
   * description  获取本月第一天的日期
   * date 2021/7/21 10:39 AM
   *
   * @param
   * @return java.lang.String
   * @author lituo
   */
  public static String getFirstDayByThisMth() {
    String curDt = DateTimeUtil.getDate8();
    return curDt.substring(0, 6) + "01";
  }

  /**
   * description  获取本月最后一天的日期
   * date 2021/7/21 10:39 AM
   *
   * @param
   * @return java.lang.String
   * @author lituo
   */
  public static String getLastDayByThisMth() {
    String curDt = DateTimeUtil.getDate8();
    return getLastDayByMth(curDt.substring(0, 6));
  }

  /**
   * description  获取某个月最后一天
   * date 2021/7/22 9:40 AM
   *
   * @param mth
   * @return java.lang.String
   * @author lituo
   */
  public static String getLastDayByMth(String mth) {
    Calendar cal = Calendar.getInstance();
    //设置年份
    cal.set(Calendar.YEAR, Integer.parseInt(mth.substring(0, 4)));
    //设置月份
    cal.set(Calendar.MONTH, Integer.parseInt(mth.substring(4, 6)) - 1);
    //获取某月最大天数
    int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    //设置日历中月份的最大天数
    cal.set(Calendar.DAY_OF_MONTH, lastDay);
    //格式化日期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String lastDayOfMonth = sdf.format(cal.getTime());
    return lastDayOfMonth;

  }

  /**
   * description 获取下个月的最后一天
   * date 2021/7/22 9:42 AM
   *
   * @param
   * @return java.lang.String
   * @author lituo
   */
  public static String getLastDayByLastMth() {
    Calendar cale = Calendar.getInstance();
    cale.add(Calendar.DAY_OF_MONTH, getDaysOfMonth(1));
    cale.set(Calendar.DAY_OF_MONTH, getDaysOfMonth(1));
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    return format.format(cale.getTime());
  }

  /**
   * description 统计某月的天数
   * date 2021/7/22 10:33 AM
   *
   * @param mth
   * @return int
   * @author lituo
   */
  public static int queryDaysByMth(String mth) {
    String firstDay = mth + "01";
    String lastDay = getLastDayByMth(firstDay);
    int days = (int) DateTimeUtil.getDistanceDay(firstDay, lastDay) + 1;
    return days;
  }

  public static void test() {
    //插入t_iot_card_mth表
    int pkgDays = 365;
    String firstDay = DateTimeUtil.getDate8();
    String lastDay = "";
    //计算出firstDay和lastDay之前间隔的月份，分别插入
    //获取开始时间当月月底的时间
    String curMthLastDay = DateTimeUtil.getLastDayByMth(firstDay.substring(0, 6));
    //获取间隔天数
    long curMthDays = DateTimeUtil.getDistanceDay(firstDay, curMthLastDay) + 1;
    if (pkgDays < (int) curMthDays) {
      //如果套餐天数小于当前日期到当前日期月月底的天数，则结束日期为开始日期+套餐天数
      lastDay = DateTimeUtil.dateCalc(firstDay, null, "D", pkgDays);
      System.out.println(firstDay + "-" + lastDay);
      return;
    } else {
      System.out.println(firstDay + "-" + curMthLastDay);
    }
    //循环获取下月开始结束日期，获取天数，若总和小于套餐天数，继续往下累，直至小于当月天数为止
    //已分配天数
    int useDays = (int) curMthDays;
    //计算开始时间
    String calFirstDays = firstDay;
    String lastMth = "";
    int useDays2 = 0;
    do {
      //获取下个月
      lastMth = DateTimeUtil.dateCalc(calFirstDays, null, "M", 1).substring(0, 6);
      //获取下个月第一天
      String lastMthFirstDay = lastMth + "01";
      String lastMthLastDay = DateTimeUtil.getLastDayByMth(lastMth);
      //获取下个月天数
      int days = DateTimeUtil.queryDaysByMth(lastMth);
      //已经分配的天数
      useDays2 = useDays + days;
      if (pkgDays <= useDays2) {
        //获取最后一个月第一天
        lastMthFirstDay = lastMth + "01";
        //获取最后一个月剩余天数
        days = pkgDays - useDays;
        String lastday = DateTimeUtil.dateCalc(lastMthFirstDay, null, "D", days);
        System.out.println(lastMthFirstDay + "-" + lastday);
        break;
      }
      System.out.println(lastMthFirstDay + "-" + lastMthLastDay);
      calFirstDays = lastMthFirstDay;
      useDays = useDays2;
    } while (1 == 1);

    //处理最后一个月
//    if(pkgDays>useDays){
//      //获取最后一个月第一天
//      String lastMthFirstDay = lastMth+"01";
//      //获取最后一个月剩余天数
//      int days =  pkgDays-useDays;
//      String lastday = DateTimeUtil.dateCalc(lastMthFirstDay,null,"D",days);
//      System.out.println(lastMthFirstDay+"-"+lastday);
//    }
  }


  public static void main(String[] args) {
//    String firstDay = DateTimeUtil.getDate8();
    System.out.println(DateTimeUtil.dateCalc("20220328",null,"D",45));
//    test();
//    System.out.println(getFirstDayByNextMth());
//    System.out.println(Math.round(DateTimeUtil.getDistanceHour("20220210120000", "20220211113712")*100)/100);
//
//    BigDecimal b = new BigDecimal(DateTimeUtil.getDistanceHour("20220210120000", "20220211113712"));
//    double d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//    System.out.println(d);
//    long s = 30L;
//    System.out.println(new Double(s)/60);
  }
}
